package com.yuanfeng.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yuanfeng.business.dto.ManageShopExpressAddDTO;
import com.yuanfeng.business.dto.ManageShopExpressAddParamDTO;
import com.yuanfeng.business.dto.UserTokenBeanDTO;
import com.yuanfeng.business.entity.ShopExpressEntity;
import com.yuanfeng.business.feign.BaseServiceClient;
import com.yuanfeng.business.mapper.ShopExpressMapper;
import com.yuanfeng.business.service.ShopExpressService;
import com.yuanfeng.business.vo.ManageExpressQueryVO;
import com.yuanfeng.business.vo.ManageShopExpressQueryVO;
import com.yuanfeng.commoms.aspect.UserPermissionsResult;
import com.yuanfeng.commoms.aspect.UserService;
import com.yuanfeng.commoms.config.PermissionsConstant;
import com.yuanfeng.commoms.constant.CommonStatusEnum;
import com.yuanfeng.commoms.dto.base.BaseExpressDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @ClassName ShopExpressServiceImpl
 * @Description 店铺物流
 * @Author cf
 * @Date 2023/7/5 9:46
 * @Version 1.0
 */
@Service
public class ShopExpressServiceImpl extends ServiceImpl<ShopExpressMapper, ShopExpressEntity> implements ShopExpressService {

    @Autowired
    private ShopExpressMapper shopExpressMapper;

    @Autowired
    private BaseServiceClient baseServiceClient;

    @Override
    public ResponseResult queryShopExpress(UserTokenBeanDTO param) {

        List<ManageExpressQueryVO> allExpress = Lists.newArrayList();
        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService.judgePermissions(PermissionsConstant.SERLLER_GOODS,
                param.getToken());

        if (userPermissionsResult.getFlag()) {
            List<ManageShopExpressQueryVO> shopExpresss = shopExpressMapper
                    .selectByShopId(Integer.valueOf(userPermissionsResult.getShopId()));
            allExpress = baseServiceClient.queryNoTokenTwo();
            for (ManageExpressQueryVO manageExpressQueryResult : allExpress) {
                for (ManageShopExpressQueryVO shopExpress : shopExpresss) {
                    if (manageExpressQueryResult.getExpressId().equals(shopExpress.getExpressId())) {
                        manageExpressQueryResult.setFlag(1);
                        manageExpressQueryResult.setUserExpressId(shopExpress.getUserExpressId());
                    }
                }
            }

        } else {
            return ResponseResult.fail(CommonStatusEnum.FAIL.getCode(), BizCodeEnume.INSUFFICIENT_PERMISSIONS.getMsg());
        }
        return ResponseResult.success(allExpress);
    }

    @Override
    @Transactional
    public ResponseResult addShopExpress(ManageShopExpressAddDTO param) {
        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService.judgePermissions(PermissionsConstant.SERLLER_GOODS,
                param.getToken());

        if (userPermissionsResult.getFlag()) {
            List<ManageShopExpressAddParamDTO> shopExpressList = param.getShopExpressList();
            List<ManageShopExpressAddParamDTO> shopExpressAdd = Lists.newArrayList();
            List<ManageShopExpressAddParamDTO> shopExpressUpdate = Lists.newArrayList();
            List<Integer> userExpressIds = Lists.newArrayList();
            for (ManageShopExpressAddParamDTO shopExpress : shopExpressList) {
                shopExpress.setShopId(Integer.valueOf(userPermissionsResult.getShopId()));

                if (null == shopExpress.getWaybillTplId() || shopExpress.getWaybillTplId().equals("")) {
                    shopExpress.setWaybillTplId(0);
                }

                if (null == shopExpress.getUserIsDefault() || shopExpress.getUserIsDefault().equals("")) {
                    shopExpress.setUserIsDefault(0);
                }

                if (null == shopExpress.getUserTplItem() || shopExpress.getUserTplItem().equals("")) {
                    shopExpress.setUserTplItem("0");
                }

                if (null == shopExpress.getUserExpressId() || shopExpress.getUserExpressId().equals("")) {
                    shopExpressAdd.add(shopExpress);
                } else {
                    userExpressIds.add(shopExpress.getUserExpressId());
                    shopExpressUpdate.add(shopExpress);
                }
            }

            // 删除不包含的快递公司

            if (!CollectionUtils.isEmpty(userExpressIds)) {
                shopExpressMapper.deleteBatch(userExpressIds, Integer.valueOf(userPermissionsResult.getShopId()));
            }

            if (!CollectionUtils.isEmpty(shopExpressAdd)) {
                shopExpressMapper.insertBatch(shopExpressAdd);
            }
            if (!CollectionUtils.isEmpty(shopExpressUpdate)) {
                shopExpressMapper.updateBatch(shopExpressUpdate);
            }

        } else {
            return ResponseResult.fail(CommonStatusEnum.FAIL.getCode(), BizCodeEnume.INSUFFICIENT_PERMISSIONS.getMsg());
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult queryShopAllExpress(UserTokenBeanDTO param) {
        UserInfoVO ui = ParseTokenUtils.getUserInfoByToken(param.getToken());
        if (ui == null) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
        }
        //查询店铺默认快递公司
        List<BaseExpressDTO> shopExpress = this.baseMapper.selectShopExpress(Integer.valueOf(ui.getShopId()));
        return ResponseResult.success(shopExpress);
    }


}
